package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class TypeSalesStrategy implements SalesStrategy {
    private String itemType;

    public TypeSalesStrategy(String itemType) {
        this.itemType = itemType;
    }

    @Override
    public double calculateSales(List<Order> orders) {
        return orders.stream()
                .flatMap(order -> order.getOrderDetails().stream())
                .filter(detail -> detail.getItem().getItemType().equals(itemType))
                .mapToDouble(detail -> detail.getItem().getPrice() * detail.getQuantity())
                .sum();
    }



    @Override
    public double calculateSalesFromDB(Connection connection) throws Exception {
        String query = "SELECT SUM(od.Quantity * mi.Price) AS TotalSales " +
                "FROM OrderDetails od " +
                "JOIN MenuItems mi ON od.ItemID = mi.ItemID " +
                "WHERE mi.ItemType = ?";
        PreparedStatement statement = connection.prepareStatement(query);
        statement.setString(1, itemType);
        ResultSet resultSet = statement.executeQuery();

        if (resultSet.next()) {
            return resultSet.getDouble("TotalSales");
        }
        return 0;
    }
}
